﻿<UserControl x:Class="XamlHelpmeet.UI.Editors.TextBoxEditor"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:cvtrs="clr-namespace:XamlHelpmeet.UI.Converters"
             xmlns:local="clr-namespace:XamlHelpmeet.UI.Editors"
             xmlns:rules="clr-namespace:XamlHelpmeet.UI.ValidationRules"
             xmlns:utils="clr-namespace:XamlHelpmeet.UI.Utilities"
             Loaded="TextBlockEditor_Loaded">
    <UserControl.Resources>

        <cvtrs:BindingModeEnumConverter x:Key="bindingModeEnumConverter" />
        <cvtrs:PropertyTypeNameConverter x:Key="propertyTypeNameConverter" />

        <ObjectDataProvider x:Key="bindingModeDS"
                            MethodName="GetSortedEnumNames"
                            ObjectType="{x:Type utils:UIHelpers}">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="BindingMode" />
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>

        <ObjectDataProvider x:Key="SampleFormats"
                            MethodName="GetSampleFormats"
                            ObjectType="{x:Type utils:UIHelpers}" />

        <Style TargetType="{x:Type StackPanel}">
            <Setter Property="Margin" Value="0,10,0,0" />
        </Style>

        <ControlTemplate x:Key="validationTemplate">
            <DockPanel>
                <AdornedElementPlaceholder x:Name="errorAdorner" />
                <TextBlock Margin="2,0,5,0"
                           VerticalAlignment="Center"
                           FontSize="20"
                           Foreground="Red"
                           Text="*"
                           ToolTip="{Binding ElementName=errorAdorner,
                                             Path=AdornedElement.(Validation.Errors)[0].ErrorContent}" />
            </DockPanel>
        </ControlTemplate>

        <Style TargetType="{x:Type TextBox}">
            <Setter Property="Validation.ErrorTemplate" Value="{StaticResource validationTemplate}" />
        </Style>

    </UserControl.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <StackPanel Orientation="Horizontal">
            <TextBlock Width="50" Text="Path" />
            <Grid>
                <TextBox x:Name="txtBindingPath"
                         Width="100"
                         VerticalAlignment="Top"
                         Text="{Binding BindingPath}" />
                <ComboBox x:Name="cboBindingPath"
                          MinWidth="100"
                          VerticalAlignment="Top"
                          SelectedValue="{Binding BindingPath}"
                          SelectedValuePath="Name"
                          Visibility="Collapsed">
                    <ComboBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=Name}" />
                        </DataTemplate>
                    </ComboBox.ItemTemplate>
                </ComboBox>
            </Grid>

        </StackPanel>

        <StackPanel Grid.Row="1" Orientation="Horizontal">

            <TextBlock Margin="0,0,4,0"
                       HorizontalAlignment="Left"
                       Text="{Binding ElementName=cboBindingPath,
                                      Path=SelectedItem.PropertyTypeName,
                                      Mode=OneWay,
                                      Converter={StaticResource propertyTypeNameConverter}}" />

            <TextBlock HorizontalAlignment="Left" Text="Format" />
            <Grid>
                <ComboBox x:Name="cboStringFormat"
                          Height="{Binding ActualHeight,
                                           ElementName=txtStringFormat,
                                           Mode=OneWay}"
                          MaxWidth="117"
                          Focusable="False"
                          ItemsSource="{Binding Mode=OneWay, Source={StaticResource SampleFormats}}"
                          SelectedValuePath="StringFormatParsedValue"
                          SelectionChanged="cboStringFormat_SelectionChanged">

                    <ComboBox.GroupStyle>
                        <GroupStyle>
                            <GroupStyle.HeaderTemplate>
                                <DataTemplate>
                                    <TextBlock Margin="0,5,0,0"
                                               Background="AntiqueWhite"
                                               FontWeight="Bold"
                                               Padding="2,0,0,0"
                                               Text="{Binding Path=Name}" />
                                </DataTemplate>
                            </GroupStyle.HeaderTemplate>
                        </GroupStyle>
                    </ComboBox.GroupStyle>
                </ComboBox>

                <TextBox x:Name="txtStringFormat"
                         Width="100"
                         Margin="0,0,17,0"
                         VerticalAlignment="Top"
                         TextChanged="FormatChanged" />
            </Grid>

        </StackPanel>

        <StackPanel Grid.Row="2" Orientation="Horizontal">

            <TextBlock Width="50"
                       HorizontalAlignment="Left"
                       Text="Mode" />
            <ComboBox Width="Auto"
                      Focusable="False"
                      FontSize="10"
                      IsSynchronizedWithCurrentItem="True"
                      ItemsSource="{Binding Mode=OneWay,
                                            Source={StaticResource bindingModeDS}}"
                      SelectedValue="{Binding Path=BindingMode,
                                              Mode=TwoWay,
                                              Converter={StaticResource bindingModeEnumConverter}}" />

            <TextBlock Margin="10,0,5,0"
                       HorizontalAlignment="Left"
                       Text="Max Length" />
            <TextBox Width="50" VerticalAlignment="Top">
                <TextBox.Text>
                    <Binding Mode="TwoWay"
                             Path="MaximumLength"
                             StringFormat="{}{0:D}"
                             TargetNullValue=""
                             UpdateSourceTrigger="LostFocus"
                             ValidatesOnDataErrors="True"
                             ValidatesOnExceptions="True">
                        <Binding.ValidationRules>
                            <rules:PositiveIntegerValidationRule />
                        </Binding.ValidationRules>
                    </Binding>
                </TextBox.Text>
            </TextBox>

            <TextBlock Margin="15,0,5,0"
                       HorizontalAlignment="Left"
                       Text="Width" />
            <TextBox Width="50"
                     Margin="0,0,10,0"
                     VerticalAlignment="Top">
                <TextBox.Text>
                    <Binding Mode="TwoWay"
                             Path="Width"
                             UpdateSourceTrigger="LostFocus"
                             ValidatesOnDataErrors="True"
                             ValidatesOnExceptions="True">
                        <Binding.ValidationRules>
                            <rules:PositiveIntegerValidationRule />
                        </Binding.ValidationRules>
                    </Binding>
                </TextBox.Text>
            </TextBox>

        </StackPanel>


    </Grid>
</UserControl>
